package top.hiai.Controller.admin;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;

import top.hiai.interceptor.AdminInterceptor;
import top.hiai.model.User;
import top.hiai.service.ArticleService;
import top.hiai.service.UserService;
import top.hiai.util.Md5Util;
import top.hiai.util.StringUtil;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top,开发者：黄康权
* @Introduction 后台所有页面的渲染
*/

public class AdminController extends Controller {
	
	UserService userService = new UserService();
	ArticleService articleService = new ArticleService();
	/**
	 * 渲染后台主界面
	 */
	@Before(AdminInterceptor.class)
	public void adminWelcome(){
		adminMessageCount();
		render("adminWelcome.html");
	}
	/**
	 * 渲染管理员后台登录界面
	 */
	public void login(){
		render("adminLogin.html");
	}
	/**
	 * 渲染后台资源管理界面
	 */
	@Before(AdminInterceptor.class)
	public void adminArticleManage(){
		render("adminArticleManage.html");
	}
	/**
	 * 渲染后台资源类别管理界面
	 */
	@Before(AdminInterceptor.class)
	public void adminArticleTypeManage(){
		render("adminArticleTypeManage.html");
	}
	/**
	 * 渲染后台用户管理界面
	 */
	@Before(AdminInterceptor.class)
	public void adminUserManage(){
		render("adminUserManage.html");
	}
	/**
	 * 渲染后台评论管理界面
	 */
	@Before(AdminInterceptor.class)
	public void adminCommentManage(){
		render("adminCommentManage.html");
	}
	/**
	 * 渲染后台友情链接管理界面
	 */
	@Before(AdminInterceptor.class)
	public void adminLinkManage(){
		render("adminLinkManage.html");
	}
	/**
	 * 渲染后台修改密码界面
	 */
	@Before(AdminInterceptor.class)
	public void adminUpdatePas(){
		render("adminUpdatePas.html");
	}
	
	/**
	 * 管理员登录验证
	 * 1.验证用户名和密码不能为空
	 * 2.验证用户名是否存在
	 * 3.判断密码是否正确
	 * 4.设置Session
	 * 5.跳转到后台主界面
	 * @throws UnsupportedEncodingException 
	 * @throws NoSuchAlgorithmException 
	 */
	public void loginCheck() throws NoSuchAlgorithmException, UnsupportedEncodingException{
		User user;
		// 初始化数据字典Map
        String email = getPara("email");
        String password = getPara("password");
        //System.out.println(email);
        //1.验证用户名和密码不能为空
        if(StringUtil.isEmpty(email)){
        	setAttr("errorMsg", "用户名不能为空");
            render("adminLogin.html");
            return;
        }
        if(StringUtil.isEmpty(password)){
        	setAttr("errorMsg", "密码不能为空");
            render("adminLogin.html");
            return;
        }
        password = Md5Util.EncoderPwdByMd5(password);
        //2.验证用户名是否存在
        user = userService.findUserByNameAndPassword(email);
        if(user != null){
        	//3.判断密码是否正确
        	if(password.equals(user.getStr("password"))){	//验证密码的正确性
        		if("管理员".equals(user.getStr("role_name"))){
    				// 将用户信息保存到缓存，用作超时判断
    				setSessionAttr("LoginAdminCache",user);
    				redirect("/admin/adminWelcome");
        		}else{
                	setAttr("errorMsg", "您没有权限访问");
                    render("adminLogin.html");
                    return;
        		}
			}else{
            	setAttr("errorMsg", "邮箱或密码输入错误，请重新输入。");
                render("adminLogin.html");
                return;
			}
        }else{
        	setAttr("errorMsg", "该用户不存在");
            render("adminLogin.html");
            return;
        }
	}
	/**
	 * 统计信息，文章统计，待审核文章统计，用户统计
	 */
	public void adminMessageCount(){
		setAttr("articleCounts",articleService.getArticleCounts());
		setAttr("articleStateCounts",articleService.getArticleStateCounts());
		setAttr("UserCounts",userService.getUserCounts());
	}
}
